﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Matrix2D.Forms
{
    public partial class FormMain : Form
    {

        GraphicsFramework.Drawing.Base[] selected;

        public FormMain()
        {
            InitializeComponent();

            MainCanvas.CommandUpdated += new CanvasGDI.CanvasGDI.CommandUpdatedDelegate(CommandUpdated);
            MainCanvas.MouseUpdated += new CanvasGDI.CanvasGDI.MousePositionUpdatedDelegate(MouseUpdated);
        }

        private void FormLoad(object sender, EventArgs e)
        {
            this.Hide();
            Application.DoEvents();

            //Size and location
            this.Location = Properties.Settings.Default.Location;
            this.Size = Properties.Settings.Default.Size;
            this.WindowState = (FormWindowState)Properties.Settings.Default.WindowState;

            MainCanvas.AntiAlias = true;
            MainCanvas.GridValue = new GraphicsFramework.Entity.Point2F(1, 1);
            OpenGLCanvas.GridValue = new GraphicsFramework.Entity.Point2F(1, 1);

            //Toolbars
            InitializeToolbars();

            this.Show();
        }

        private void InitializeToolbars()
        {
            //Set location
            ToolStandard.Location = new Point(0, 0);
            ToolZoom.Location = new Point(ToolStandard.Width + 1, 0);
            ToolParagraph.Location = new Point(ToolZoom.Location.X + ToolZoom.Width + 1, 0);
            ToolDraw.Location = new Point(ToolParagraph.Location.X + ToolParagraph.Width + 1, 0);
            ToolShape.Location = new Point(ToolDraw.Location.X + ToolDraw.Width + 1, 0);

            //Fonts
            FontFamily[] fonts = FontFamily.Families;
            foreach (FontFamily font in fonts)
            {
                ToolFonts.Items.Add(font.Name);
            }

            //Font Size
            for (int i = 6; i <= 100; i++)
            {
                ToolFontSize.Items.Add(i);
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);

            Properties.Settings.Default.Location = this.Location;
            Properties.Settings.Default.Size = this.Size;
            Properties.Settings.Default.WindowState = (int)this.WindowState;
            Properties.Settings.Default.Save();
        }

        private void CommandUpdated(string command)
        {
            StatusCommand.Text = command;
        }

        private void MouseUpdated(float x, float y)
        {
            StatusCoordinate.Text = String.Format("{0}, {1}", x, y);
        }

        private void ViewSelectedProperties()
        {
            ToolFonts.Text = selected[0].TextProperties.Font;
            ToolFontSize.Text = selected[0].TextProperties.Size.ToString();
        }

        private void DrawingSelected(GraphicsFramework.Drawing.Base[] selected)
        {
            this.selected = selected;
            ViewSelectedProperties();
        }

        private void MainCanvas_DrawingDeselected()
        {
            selected = null;
        }

        private void ToolZoomIn_Click(object sender, EventArgs e)
        {
            MainCanvas.ZoomIn();
        }

        private void ToolZoomOut_Click(object sender, EventArgs e)
        {
            MainCanvas.ZoomOut();
        }

        private void ToolZoomWindow_Click(object sender, EventArgs e)
        {
            MainCanvas.ZoomWindow();
        }

        private void ToolZoomExtent_Click(object sender, EventArgs e)
        {
            MainCanvas.ZoomExtent();
        }

        private void ToolMove_Click(object sender, EventArgs e)
        {
            MainCanvas.Pan();
        }

        private void StatusSnap_CheckedChanged(object sender, EventArgs e)
        {
            MainCanvas.Snap = StatusSnap.Checked;
        }

        private void StatusGrid_CheckStateChanged(object sender, EventArgs e)
        {
            MainCanvas.ShowGrid = StatusGrid.Checked;
        }

        private void MainProperty_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            MainCanvas.RefreshDrawing();
        }

        private void ToolFonts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (selected != null)
            {
                foreach (GraphicsFramework.Drawing.Base drawing in selected)
                {
                    drawing.TextProperties.Font = ToolFonts.Text;
                }

                MainCanvas.RefreshDrawing();
            }
        }

        private void ToolFontSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (selected != null)
            {
                foreach (GraphicsFramework.Drawing.Base drawing in selected)
                {
                    drawing.TextProperties.Size = Convert.ToInt32(ToolFontSize.Text);
                }

                MainCanvas.RefreshDrawing();
            }
        }

        private void ToolFlipHorizontal_Click(object sender, EventArgs e)
        {
            MainCanvas.FlipHorizontal();
        }

        private void ToolFlipVertical_Click(object sender, EventArgs e)
        {
            MainCanvas.FlipVertical();
        }

        private void ToolRotateLeft_Click(object sender, EventArgs e)
        {
            MainCanvas.RotateLeft();
        }

        private void ToolRotateRight_Click(object sender, EventArgs e)
        {
            MainCanvas.RotateRight();
        }

        private void ToolSelect_Click(object sender, EventArgs e)
        {
            MainCanvas.SelectDrawing();
        }

        private void ToolDrawLine_Click(object sender, EventArgs e)
        {
            MainCanvas.StartDrawingLine();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DrawGrid();
                ImportData(dialog.FileName);
            }
        }

        private void DrawGrid()
        {
            for (int count = 1900; count <= 2020; count += 10)
            {
                MainCanvas.AddDrawing(new GraphicsFramework.Drawing.Primitive.TextBlock(count.ToString(), new GraphicsFramework.Entity.Point2F(count, -0.2F)));
                MainCanvas.AddDrawing(new GraphicsFramework.Drawing.Primitive.Line(new GraphicsFramework.Entity.Point2F(count, 0), new GraphicsFramework.Entity.Point2F(count, 8)));

                OpenGLCanvas.AddDrawing(new GraphicsFramework.Drawing.Primitive.Line(new GraphicsFramework.Entity.Point2F(count, 0), new GraphicsFramework.Entity.Point2F(count, 8)));
            }

            for (int count = 0; count < 9; count++)
            {
                MainCanvas.AddDrawing(new GraphicsFramework.Drawing.Primitive.TextBlock(count.ToString(), new GraphicsFramework.Entity.Point2F(1898, count)));
                MainCanvas.AddDrawing(new GraphicsFramework.Drawing.Primitive.Line(new GraphicsFramework.Entity.Point2F(1900, count), new GraphicsFramework.Entity.Point2F(2020, count)));

                OpenGLCanvas.AddDrawing(new GraphicsFramework.Drawing.Primitive.Line(new GraphicsFramework.Entity.Point2F(1900, count), new GraphicsFramework.Entity.Point2F(2020, count)));
            }
        }

        private void ImportData(string filename)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(filename);
            string line;
            string[] split;
            float value;

            while ((line = reader.ReadLine()) != null)
            {
                split = line.Split(',');

                value = Convert.ToSingle(split[9]);
                MainCanvas.AddDrawing(new GraphicsFramework.Drawing.Primitive.Circle(Convert.ToSingle(split[0]), value, 0.1F));
                OpenGLCanvas.AddDrawing(new GraphicsFramework.Drawing.Primitive.Circle(Convert.ToSingle(split[0]), value, 0.1F));
            }
        }

        private void ToolLeftAlign_Click(object sender, EventArgs e)
        {
            MainCanvas.LeftAlign();
        }

        private void ToolRightAlign_Click(object sender, EventArgs e)
        {
            MainCanvas.RightAlign();
        }

        private void ToolTopAlign_Click(object sender, EventArgs e)
        {
            MainCanvas.TopAlign();
        }

        private void ToolBottomAlign_Click(object sender, EventArgs e)
        {
            MainCanvas.BottomAlign();
        }

        private void ToolCenterAlignHorizontal_Click(object sender, EventArgs e)
        {
            MainCanvas.HorizontalCenterAlign();
        }

        private void ToolCenterAlignVertical_Click(object sender, EventArgs e)
        {
            MainCanvas.VerticalCenterAlign();
        }
    }
}
